package com.szyt.basicframe.util;

import com.szyt.basicframe.pojo.bo.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

public class SecurityUtils {
    public static Long getCurrentUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null || !authentication.isAuthenticated()) {
            return null;
        }

        Object principal = authentication.getPrincipal();
        if (principal instanceof UserDetails) {
            // 假设你的UserDetails实现中有getId()方法
            return ((LoginUser) principal).getUser().getId();
        }
        /*else if (principal instanceof String) {
            // 或者可能是用户名字符串
            // 这里需要根据你的实际情况处理
            return null;
        }*/
        return null;
    }
}
